home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Gfx / Edit / TSMorph / src / RGBToScreen020.c < prev    next >
C/C++ Source or Header  |  1994-10-30  |  12KB  |  476 lines

  1. // TSMorph - Amiga Morphing program
  2. // Copyright (C) © 1993  Topicsave Limited
  3.  
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // any later version.
  8.  
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13.  
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. // mpaddock@cix.compulink.co.uk
  19.  
  20. // include precompiled code if required
  21. #ifndef TSMORPH_H
  22. #include "TSMorph.h"
  23. #endif
  24.  
  25. // The two functions in this file are exact clones of those in OpalLoad.c
  26. // but This code is compiled with -m2 -O -mt
  27. // and is only called if at least a 020 is present
  28.  
  29. void
  30. RGBToScreen020(UBYTE *red,UBYTE *green,UBYTE *blue,UWORD Height,UWORD Width,
  31.                           UWORD maxcol,UWORD xadd,UBYTE *r,UBYTE *g,UBYTE *b) {
  32.     UWORD i,j,k;
  33.     ULONG maxdiff;
  34.     ULONG diff;
  35.     LONG t;
  36.     UWORD index;
  37.  
  38.     for (j = 0;
  39.           j < Height;
  40.           ++j) {
  41.         if (ProgressWnd) {
  42.           GT_SetGadgetAttrs(ProgressGadgets[GDX_Pass2],ProgressWnd,NULL,
  43.                                      GTSL_Level,(ULONG)j,TAG_END);
  44.             HandleProgressIDCMP();
  45.        }
  46.         for (i = 0;
  47.               i < Width;
  48.               ++i) {
  49.             // Find closest color
  50.             maxdiff = 0x7FFFFFFF;
  51.             for (k = 0;
  52.                   k < maxcol;
  53.                   ++k) {
  54.                 t = *red - r[k];
  55.                 diff = t*t*3;
  56.                 t = *green - g[k];
  57.                 diff += (t*t*6);
  58.                 t = *blue - b[k];
  59.                 diff += (t*t);
  60.                 if (diff < maxdiff) {
  61.                     maxdiff = diff;
  62.                     index = k;
  63.                 }
  64.             }
  65.             // Store index to color
  66.             *red = index;
  67.             ++red;
  68.             ++green;
  69.             ++blue;
  70.         }
  71.         red += xadd;
  72.         green += xadd;
  73.         blue += xadd;
  74.         if (ProgressWnd) {
  75.             HandleProgressIDCMP();
  76.         }
  77.     }
  78. }
  79.  
  80. void
  81. PaletteToScreen020(struct RastPort *Rp,struct Picture *pic,UWORD maxcol,
  82.                                 UBYTE *r,UBYTE *g,UBYTE *b) {
  83.     UWORD                    i,j,k;            // loop counters
  84.     LONG                    penno;
  85.     UBYTE                    rr,gg,bb;
  86.     ULONG maxdiff;
  87.     ULONG diff;
  88.     UWORD index;
  89.     LONG                    t;                    // temp diff
  90.     UWORD EHB;
  91.     BOOL isHAM6 = FALSE;
  92.     BOOL isHAM8 = FALSE;
  93.     BOOL isEHB = FALSE;
  94.     struct DisplayInfo queryinfo;
  95.     DisplayInfoHandle handle;
  96.     struct BitMap         BM;
  97.     UBYTE *arrayp;
  98.     ULONG NewPen[256];
  99.  
  100.    InitBitMap(&BM,pic->ilbm->Bmhd.nPlanes,pic->ilbm->Bmhd.w,pic->ilbm->Bmhd.h);
  101.       RP.BitMap = &BM;
  102.    InitBitMap(&TBM,pic->ilbm->Bmhd.nPlanes,pic->ilbm->Bmhd.w,1);
  103.    TRP.BitMap = &TBM;
  104.    TBM.Planes[0]=plane0;
  105.    TBM.Planes[1]=plane1;
  106.    TBM.Planes[2]=plane2;
  107.       TBM.Planes[3]=plane3;
  108.    TBM.Planes[4]=plane4;
  109.    TBM.Planes[5]=plane5;
  110.    TBM.Planes[6]=plane6;
  111.    TBM.Planes[7]=plane7;
  112.  
  113.     if ((handle = FindDisplayInfo(pic->ilbm->camg)) &&
  114.          (GetDisplayInfoData(handle,(UBYTE *)&queryinfo,sizeof(queryinfo),DTAG_DISP,NULL))) {
  115.         if ((pic->ilbm->Bmhd.nPlanes == 6) && (queryinfo.PropertyFlags & DIPF_IS_HAM)) {
  116.            GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  117.                                      GTTX_Text,(ULONG)MyGetMsg(MSG_REMAPH6),
  118.                                     TAG_END);
  119.             isHAM6 = TRUE;
  120.         }
  121.         else {
  122.             if ((pic->ilbm->Bmhd.nPlanes == 8) && (queryinfo.PropertyFlags & DIPF_IS_HAM)) {
  123.                 isHAM8 = TRUE;
  124.                GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  125.                                      GTTX_Text,(ULONG)MyGetMsg(MSG_REMAPH8),
  126.                                     TAG_END);
  127.             }
  128.             else {
  129.                   if ((pic->ilbm->Bmhd.nPlanes == 6) && (queryinfo.PropertyFlags & DIPF_IS_EXTRAHALFBRITE)) {
  130.                       isEHB = TRUE;
  131.                    GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  132.                                                    GTTX_Text,(ULONG)MyGetMsg(MSG_REMAPEHB),
  133.                                                 TAG_END);
  134.                   }
  135.                   else {
  136.                    GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  137.                                               GTTX_Text,(ULONG)MyGetMsg(MSG_REMAPIL),
  138.                                             TAG_END);
  139.                     for (i = 0;
  140.                         (i < pic->ilbm->ncolors);
  141.                         i++) {
  142.                         maxdiff = 0x7FFFFFFF;
  143.                         for (k = 0;
  144.                               k < maxcol;
  145.                               ++k) {
  146.                             t = pic->ilbm->RGB[i*4+1] - r[k];
  147.                             diff = t*t*3;
  148.                             t = pic->ilbm->RGB[i*4+2] - g[k];
  149.                             diff += (t*t*6);
  150.                             t = pic->ilbm->RGB[i*4+3] - b[k];
  151.                             diff += (t*t);
  152.                             if (diff < maxdiff) {
  153.                                 maxdiff = diff;
  154.                                 NewPen[i] = k;
  155.                             }
  156.                         }
  157.                     }
  158.                   }
  159.             }
  160.         }
  161.     }
  162.     for (j = 0;
  163.          j < pic->ilbm->Bmhd.h;
  164.          ++j) {
  165.         if (ProgressWnd) {
  166.           GT_SetGadgetAttrs(ProgressGadgets[GDX_Pass2],ProgressWnd,NULL,
  167.                               GTSL_Level,(ULONG)j,TAG_END);
  168.             HandleProgressIDCMP();
  169.           }
  170.           ReadPixelLine8(Rp,0,j,pic->ilbm->Bmhd.w,Array,&TRP);
  171.           arrayp = Array;
  172.         if (isHAM6) {
  173.               rr = gg = bb = 0;
  174.            for (i = 0;
  175.                     i < pic->ilbm->Bmhd.w;
  176.                  ++i) {
  177.                penno = *arrayp;
  178.                switch (penno & 0x30) {
  179.                case 0:
  180.                    rr = pic->ilbm->RGB[penno*4+1];
  181.                    gg = pic->ilbm->RGB[penno*4+2];
  182.                    bb = pic->ilbm->RGB[penno*4+3];
  183.                    break;
  184.                case 0x10:
  185.                    bb = (penno&0xf)|((penno&0xf)<<4);
  186.                    break;
  187.                case 0x20:
  188.                    rr = (penno&0xf)|((penno&0xf)<<4);
  189.                    break;
  190.                case 0x30:
  191.                    gg = (penno&0xf)|((penno&0xf)<<4);
  192.                    break;
  193.                }
  194.                 // Find closest color
  195.                 maxdiff = 0x7FFFFFFF;
  196.                 for (k = 0;
  197.                       k < maxcol;
  198.                       ++k) {
  199.                     t = rr - r[k];
  200.                     diff = t*t*3;
  201.                     t = gg - g[k];
  202.                     diff += (t*t*6);
  203.                     t = bb - b[k];
  204.                     diff += (t*t);
  205.                     if (diff < maxdiff) {
  206.                         maxdiff = diff;
  207.                         index = k;
  208.                     }
  209.                 }
  210.                 *arrayp++ = index;
  211.             }
  212.           }
  213.           else {
  214.               if (isHAM8) {
  215.                 rr = gg = bb = 0;
  216.                for (i = 0;
  217.                         i < pic->ilbm->Bmhd.w;
  218.                      ++i) {
  219.                    penno = *arrayp;
  220.                    switch (penno & 0xc0) {
  221.                    case 0:
  222.                        rr = pic->ilbm->RGB[penno*4+1];
  223.                        gg = pic->ilbm->RGB[penno*4+2];
  224.                        bb = pic->ilbm->RGB[penno*4+3];
  225.                        break;
  226.                    case 0x40:
  227.                        bb = (penno&0x3f)<<2;
  228.                        break;
  229.                    case 0x80:
  230.                        rr = (penno&0x3f)<<2;
  231.                        break;
  232.                    case 0xc0:
  233.                        gg = (penno&0x3f)<<2;
  234.                        break;
  235.                    }
  236.                     // Find closest color
  237.                     maxdiff = 0x7FFFFFFF;
  238.                     for (k = 0;
  239.                           k < maxcol;
  240.                           ++k) {
  241.                         t = rr - r[k];
  242.                         diff = t*t*3;
  243.                         t = gg - g[k];
  244.                         diff += (t*t*6);
  245.                         t = bb - b[k];
  246.                         diff += (t*t);
  247.                         if (diff < maxdiff) {
  248.                             maxdiff = diff;
  249.                             index = k;
  250.                         }
  251.                     }
  252.                     *arrayp++ = index;
  253.                 }
  254.               }
  255.               else {
  256.                   if (isEHB) {
  257.                    for (i = 0;
  258.                             i < pic->ilbm->Bmhd.w;
  259.                          ++i) {
  260.                        penno = *arrayp;
  261.                        EHB = (penno & 0x20)?1:0;
  262.                        penno &= 0x1f;
  263.                        rr = pic->ilbm->RGB[penno*4+1]>>EHB;
  264.                        gg = pic->ilbm->RGB[penno*4+2]>>EHB;
  265.                        bb = pic->ilbm->RGB[penno*4+3]>>EHB;
  266.                         // Find closest color
  267.                         maxdiff = 0x7FFFFFFF;
  268.                         for (k = 0;
  269.                               k < maxcol;
  270.                               ++k) {
  271.                             t = rr - r[k];
  272.                             diff = t*t*3;
  273.                             t = gg - g[k];
  274.                             diff += (t*t*6);
  275.                             t = bb - b[k];
  276.                             diff += (t*t);
  277.                             if (diff < maxdiff) {
  278.                                 maxdiff = diff;
  279.                                 index = k;
  280.                             }
  281.                         }
  282.                         *arrayp++ = index;
  283.                     }
  284.                   }
  285.                   else {
  286.                    for (i = 0;
  287.                             i < pic->ilbm->Bmhd.w;
  288.                          ++i) {
  289.                        penno = *arrayp;
  290.                         *arrayp++ = NewPen[penno];
  291.                     }
  292.                 }
  293.             }
  294.        }
  295.        WritePixelLine8(Rp,0,j,pic->ilbm->Bmhd.w,Array,&TRP);
  296.     }
  297. }
  298.  
  299. void
  300. PaletteToEGS020(struct RastPort *Rp,struct Picture *pic,UBYTE *plane,UBYTE *r,UBYTE *g,UBYTE *b) {
  301.     UWORD    i,j;            // loop counters
  302.     LONG    penno;
  303.     UBYTE    rr,gg,bb;
  304.     UWORD EHB;
  305.     BOOL isHAM6 = FALSE;
  306.     BOOL isHAM8 = FALSE;
  307.     BOOL isEHB = FALSE;
  308.     struct DisplayInfo queryinfo;
  309.     DisplayInfoHandle handle;
  310.     struct BitMap         BM;
  311.     UBYTE *arrayp;
  312.  
  313.     // Initialise stuff
  314.    InitBitMap(&BM,pic->ilbm->Bmhd.nPlanes,pic->ilbm->Bmhd.w,pic->ilbm->Bmhd.h);
  315.       RP.BitMap = &BM;
  316.    InitBitMap(&TBM,pic->ilbm->Bmhd.nPlanes,pic->ilbm->Bmhd.w,1);
  317.    TRP.BitMap = &TBM;
  318.    TBM.Planes[0]=plane0;
  319.    TBM.Planes[1]=plane1;
  320.    TBM.Planes[2]=plane2;
  321.       TBM.Planes[3]=plane3;
  322.    TBM.Planes[4]=plane4;
  323.    TBM.Planes[5]=plane5;
  324.    TBM.Planes[6]=plane6;
  325.    TBM.Planes[7]=plane7;
  326.  
  327.     // based on CAMG chunk determine special type of image
  328.     if ((handle = FindDisplayInfo(pic->ilbm->camg)) &&
  329.          (GetDisplayInfoData(handle,(UBYTE *)&queryinfo,sizeof(queryinfo),DTAG_DISP,NULL))) {
  330.         // 6 plane HAM
  331.         if ((pic->ilbm->Bmhd.nPlanes == 6) && (queryinfo.PropertyFlags & DIPF_IS_HAM)) {
  332.            GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  333.                                      GTTX_Text,(ULONG)"Converting HAM6 to EGS",
  334.                                     TAG_END);
  335.             isHAM6 = TRUE;
  336.         }
  337.         else {
  338.             // 8 plane HAM
  339.             if ((pic->ilbm->Bmhd.nPlanes == 8) && (queryinfo.PropertyFlags & DIPF_IS_HAM)) {
  340.                 isHAM8 = TRUE;
  341.                GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  342.                                      GTTX_Text,(ULONG)"Converting HAM8 to EGS",
  343.                                     TAG_END);
  344.             }
  345.             else {
  346.                 // 6 plane EHB
  347.                   if ((pic->ilbm->Bmhd.nPlanes == 6) && (queryinfo.PropertyFlags & DIPF_IS_EXTRAHALFBRITE)) {
  348.                       isEHB = TRUE;
  349.                    GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  350.                                                    GTTX_Text,(ULONG)"Converting EHB to EGS",
  351.                                                 TAG_END);
  352.                   }
  353.                   else {
  354.                       // standard ILBM
  355.                    GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  356.                                               GTTX_Text,(ULONG)"Converting ILBM to EGS",
  357.                                             TAG_END);
  358.                   }
  359.             }
  360.         }
  361.     }
  362.     // for each line
  363.     for (j = 0;
  364.          j < pic->ilbm->Bmhd.h;
  365.          ++j) {
  366.         if (ProgressWnd) {
  367.           GT_SetGadgetAttrs(ProgressGadgets[GDX_Pass2],ProgressWnd,NULL,
  368.                               GTSL_Level,(ULONG)j,TAG_END);
  369.             HandleProgressIDCMP();
  370.           }
  371.           // convert line to chunky
  372.           ReadPixelLine8(Rp,0,j,pic->ilbm->Bmhd.w,Array,&TRP);
  373.           arrayp = Array;
  374.           // for each pixel depending on mode convert to EGS
  375.         if (isHAM6) {
  376.             // rgb = 0 at start of line
  377.               rr = gg = bb = 0;
  378.            for (i = 0;
  379.                     i < pic->ilbm->Bmhd.w;
  380.                  ++i) {
  381.                penno = *arrayp;
  382.                switch (penno & 0x30) {    // HAM selection
  383.                case 0:
  384.                    rr = pic->ilbm->RGB[penno*4+1];
  385.                    gg = pic->ilbm->RGB[penno*4+2];
  386.                    bb = pic->ilbm->RGB[penno*4+3];
  387.                    break;
  388.                case 0x10:
  389.                    bb = (penno&0xf)|((penno&0xf)<<4);
  390.                    break;
  391.                case 0x20:
  392.                    rr = (penno&0xf)|((penno&0xf)<<4);
  393.                    break;
  394.                case 0x30:
  395.                    gg = (penno&0xf)|((penno&0xf)<<4);
  396.                    break;
  397.                }
  398.                *plane++ = rr;
  399.                *plane++ = gg;
  400.                *plane++ = bb;
  401.                plane++;
  402.                arrayp++;
  403.             }
  404.           }
  405.           else {
  406.               if (isHAM8) {
  407.                   // very similar to HAM6
  408.                 rr = gg = bb = 0;
  409.                for (i = 0;
  410.                         i < pic->ilbm->Bmhd.w;
  411.                      ++i) {
  412.                    penno = *arrayp;
  413.                    switch (penno & 0xc0) {
  414.                    case 0:
  415.                        rr = pic->ilbm->RGB[penno*4+1];
  416.                        gg = pic->ilbm->RGB[penno*4+2];
  417.                        bb = pic->ilbm->RGB[penno*4+3];
  418.                        break;
  419.                    case 0x40:
  420.                        bb = (penno&0x3f)<<2;
  421.                        break;
  422.                    case 0x80:
  423.                        rr = (penno&0x3f)<<2;
  424.                        break;
  425.                    case 0xc0:
  426.                        gg = (penno&0x3f)<<2;
  427.                        break;
  428.                    }
  429.                    *plane++ = rr;
  430.                    *plane++ = gg;
  431.                    *plane++ = bb;
  432.                    plane++;
  433.                    arrayp++;
  434.                 }
  435.               }
  436.               else {
  437.                   if (isEHB) {
  438.                       // simple - just check highest plane and divide by two if required
  439.                    for (i = 0;
  440.                             i < pic->ilbm->Bmhd.w;
  441.                          ++i) {
  442.                        penno = *arrayp;
  443.                        EHB = (penno & 0x20)?1:0;
  444.                        penno &= 0x1f;
  445.                        rr = pic->ilbm->RGB[penno*4+1]>>EHB;
  446.                        gg = pic->ilbm->RGB[penno*4+2]>>EHB;
  447.                        bb = pic->ilbm->RGB[penno*4+3]>>EHB;
  448.                        *plane++ = rr;
  449.                        *plane++ = gg;
  450.                        *plane++ = bb;
  451.                        plane++;
  452.                        arrayp++;
  453.                     }
  454.                   }
  455.                   else {
  456.                       // simple palette remap
  457.                    for (i = 0;
  458.                             i < pic->ilbm->Bmhd.w;
  459.                          ++i) {
  460.                        penno = *arrayp;
  461.                        rr = pic->ilbm->RGB[penno*4+1];
  462.                        gg = pic->ilbm->RGB[penno*4+2];
  463.                        bb = pic->ilbm->RGB[penno*4+3];
  464.                        *plane++ = rr;
  465.                        *plane++ = gg;
  466.                        *plane++ = bb;
  467.                        plane++;
  468.                        arrayp++;
  469.                     }
  470.                 }
  471.             }
  472.        }
  473.         plane += (pic->EGS_BitMap->BytesPerRow - (4*pic->ilbm->Bmhd.w));
  474.     }
  475. }
  476.